//
//  AreaPickView.swift
//  WineComing
//
//  Created by xiaozhi on 15/7/12.
//  Copyright (c) 2015年 xiaozhi. All rights reserved.
//

import UIKit

class AreaPickView: UIView {
    @IBOutlet var pickview:UIPickerView!
    @IBOutlet var contentView:UIView!
    @IBOutlet var cancelBtn:UIButton!
    @IBOutlet var okBtn:UIButton!
    @IBOutlet var contentViewTop:NSLayoutConstraint!
    @IBAction func dismissSelf(){
        self.hiddenPickView()
    }
    var provinces:[ProviceObj] = []
    var cities:[CityObj] = []
    var areas:[DistrictObj]? = []
    var info:(provice:ProviceObj?,city:CityObj?,district:DistrictObj?)
    var setArea:((provice:ProviceObj?,city:CityObj?,district:DistrictObj?)->Void)?
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    @IBAction func cancelBtnClick()->Void{
        self.hiddenPickView()
    }
    @IBAction func okBtnClick()->Void{
        setArea?(provice: info.0,city: info.1,district: info.2)
        self.hiddenPickView()
    }
    func showPickView()->Void{
        self.hidden = false
        contentViewTop.constant = 0
        UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.LayoutSubviews, animations:{self.layoutIfNeeded()}, completion: nil)
    }
    func hiddenPickView()->Void{
        contentViewTop.constant = contentView.bounds.size.height
        UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.LayoutSubviews, animations:{self.layoutIfNeeded()}, completion: { finished in self.hidden = true})
    }
    required init?(coder aDecoder: NSCoder) {
        provinces = GlobalDataHelper.instance.proviceObjArray!
        cities = provinces[0].sub!
        areas = cities[0].sub
        info.0 = provinces[0]
        info.1 = cities[0]
        info.2 =  nil
        if areas?.count>0{
            info.2 = areas![0]
        }
        super.init(coder: aDecoder);
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        self.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.3)
        okBtn.setTitleColor(commonRedColor, forState: UIControlState.Normal)
    }
    
}
extension AreaPickView:UIPickerViewDataSource,UIPickerViewDelegate{
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
        return 3
    }
    
    // returns the # of rows in each component..
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        switch component{
        case 0:
            return provinces.count
        case 1:
            return cities.count
        case 2:
            return areas!.count
        default:break
        }
        return 0
    }
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
        switch component{
        case 0:
            return provinces[row].name
        case 1:
            return cities[row].name
        case 2:
            if areas?.count>0{
                return areas![row].name
            }
            else{
                return ""
            }
        default:break
        }
        return ""
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
        switch component{
        case 0:
            cities = provinces[row].sub!
            pickerView.selectRow(0, inComponent: 1, animated: true)
            pickerView.reloadComponent(1)
            info.0 = provinces[row]
            
            areas = cities[0].sub
            info.1 = cities[0]
            info.2 =  nil
            if areas?.count>0{
                info.2 = areas![0]
            }
            pickerView.selectRow(0, inComponent: 2, animated: true)
            pickerView.reloadComponent(2)
        case 1:
            areas = cities[row].sub!
            pickerView.selectRow(0, inComponent: 2, animated: true)
            pickerView.reloadComponent(2)
            info.1 =  cities[row]
            info.2 = nil
            if areas?.count>0{
                info.2 = areas![0]
            }
        case 2:
            info.2 = nil
            if areas?.count>0{
                info.2 = areas![row]
            }
            break
        default:break
        }
        print(info)
    }
}